package main

import (
	"context"
	"fmt"

	"hertzx/config"
	"hertzx/middleware"
	"hertzx/router"

	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/app/server"
	"github.com/cloudwego/hertz/pkg/common/hlog"
)

func Init(h *server.Hertz) {
	middleware.InitCSRF(h)
	middleware.InitCors(h)
}

func main() {
	/*
		基本埋点
		HTTPStart : 请求开始
		HTTPFinish : 请求结束
	*/

	h1 := config.NewHttp2("0.0.0.0:4242")
	h3 := config.NewQuic("0.0.0.0:4243")

	Init(h1)
	Init(h3)
	h1.Use(func(c context.Context, ctx *app.RequestContext) {
		ctx.Request.SetHeader("Alt-Svc", fmt.Sprintf(`h3=":4243"`))
		ctx.Next(c)
		//host, _, err := net.SplitHostPort(string(ctx.Request.Host()))
		//if err != nil {
		//	panic(err)
		//}
		ctx.Response.Header.Set("Alt-Svc", fmt.Sprintf(`h3="0.0.0.0:4243"`))
	})
	hlog.SetLevel(hlog.LevelTrace)
	for _, iRouter := range router.InitRouterx() {
		iRouter.InitRoute(h1)
		iRouter.InitRoute(h3)
	}

	go h1.Run()
	h3.Run()
}
